//
//  RongCloudService.m
//  Molian
//
//  Created by wan song on 6/1/15.
//  Copyright (c) 2015 memeda. All rights reserved.
//

#import "RongCloudService.h"


@implementation RongCloudService

+ (void)reconnecToRongCloud {
    /*
    // 调用云代码重新获取融云的token并重连
    MLUser *user = [MLUser currentUser];
    NSDictionary * params = @{@"userId": user.objectId};
    [AVCloud callFunctionInBackground:@"getRongCloudToken" withParameters:params block:^(id object, NSError *error) {
        if (!error) {
            NSString *rc_token = (NSString*)object;
            // 连接融云
            [RCIM connectWithToken:rc_token completion:^(NSString *userId) {
                NSLog(@"Rong Cloud Login successfully with userId: %@.", userId);
            } error:^(RCConnectErrorCode status) {
                NSLog(@"Rong Cloud token error");
            }];
            // 写入新的json数据到plist文件中
            NSDictionary *token_dict = @{@"rc_user": user.objectId, @"rc_token": rc_token};
            [Utils saveToDefaults:token_dict forKey:@"rc_info"];
        }
    }];
     */
}

+ (void)connectToRongCloud {
    /*
    // 连接融云的IM服务器
    // 融云的TOKEN存储在plist文件里里面,如果TOKEN不存在或者失效则重新调用云代码获取这个TOKEN
    NSDictionary*rc_info = [Utils defaultsForKey:@"rc_info"];
    if (rc_info) {
        NSString *rc_token = [rc_info objectForKey:@"rc_token"];
        NSString *rc_user = [rc_info objectForKey:@"rc_user"];
        
        MLUser *user = [MLUser currentUser];
        if ([user.objectId isEqualToString:rc_user]) {
            // 用户一直,可以连接
            // 连接融云服务器
            [RCIM connectWithToken:rc_token completion:^(NSString *userId) {
                // 此处处理连接成功。
                NSLog(@"Rong Cloud Login successfully with userId: %@.", userId);
            } error:^(RCConnectErrorCode status) {
                // 此处处理连接错误。
                NSLog(@"Rong Cloud Login failed.");
                // 调用云代码重新获取融云的token并重连
                [self reconnecToRongCloud];
            }];
        } else {
            [self reconnecToRongCloud];
        }
        
    } else {
        // token文件不存在,请求云代码获取token并保存到文件中
        [self reconnecToRongCloud];
    }
     */
}

@end
